
package data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author Alejandro
 */
public class RegistroEntregasAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        RegistroEntregasForm myForm = (RegistroEntregasForm) form;
        String codigoEntrega = myForm.getCodigo();
        
        BitacoraDataMapper mapper = new BitacoraDataMapper();
        Blob archivo = null;
        ServletOutputStream out = response.getOutputStream();
        ResultSet rs = mapper.obtenerArchivo(codigoEntrega);
        if (rs.next()) {
            archivo = rs.getBlob(4);
            response.setContentType(rs.getString(2));
            response.setHeader("Content-disposition","attachment; filename=\""+rs.getString(1)+"\"");
            InputStream in = archivo.getBinaryStream();
            int length = (int) archivo.length();
            int bufferSize = length;
            byte[] buffer = new byte[bufferSize];
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.flush();
        }
        
        return mapping.findForward(SUCCESS);
    }
}
